package com.student.system.service.impl;

import com.student.system.dao.StudentDao;
import com.student.system.dao.TeacherDao;
import com.student.system.entity.*;
import com.student.system.service.TeacherService;
import com.student.system.utils.MD5Utils;
import com.student.system.vo.MyPageInfo;
import com.student.system.wapper.StudentMessageWrapper;
import com.student.system.wapper.StudentScoreWrapper;
import com.student.system.wapper.Wrapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherServiceImpl implements TeacherService {
    private TeacherDao teacherDao = new TeacherDao();
    private StudentDao studentDao = new StudentDao();
    /**
     * 用户注册
     * @param req
     * @param resp
     */
    @Override
    public void doRegister(HttpServletRequest req, HttpServletResponse resp) {
        Teacherregister teacherregister = new Teacherregister();
        teacherregister.setUserName(req.getParameter("userName"));
        teacherregister.setPhone(req.getParameter("phone"));
        teacherregister.setPassword(MD5Utils.md5plus(req.getParameter("password")));

        try {
            teacherDao.save(teacherregister);
            resp.getWriter().print("ok");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            resp.getWriter().print("error");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doMessage(HttpServletRequest req, HttpServletResponse resp) {
        Teachermessage teachermessage = new Teachermessage();
        teachermessage.setName(req.getParameter("name"));
        teachermessage.setTeacherId(req.getParameter("teacherId"));
        teachermessage.setPhone(req.getParameter("phone"));
        teachermessage.setIdentity(req.getParameter("identity"));
        teachermessage.setSex(req.getParameter("sex"));
        try{
            teachermessage.setAge(Integer.parseInt(req.getParameter("age")));
        }catch (Exception e){}

        teachermessage.setEmail(req.getParameter("email"));
        teachermessage.setMajor(req.getParameter("major"));
        teachermessage.setIdentityId(req.getParameter("identityId"));
        try {
            teacherDao.savem(teachermessage);
            resp.getWriter().print("ok");
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            resp.getWriter().print("error");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void doLogin(HttpServletRequest req, HttpServletResponse resp) {
        String username = req.getParameter("userName");
        try {
            Teacherregister teacherregister = teacherDao.findByusername(username);
            if(teacherregister==null){
                resp.getWriter().print("用户不存在，请重新注册");
                return;
            }
            String password = req.getParameter("password");
            if(!MD5Utils.md5plus(password).equals(teacherregister.getPassword())){
                resp.getWriter().print("账号密码错误，请重新输入");
                return;
            }
            resp.getWriter().print("ok");
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void checkLoginId(HttpServletRequest req, HttpServletResponse resp) {
        String loginId= req.getParameter("userName");
        try {
            boolean exist = teacherDao.checkLoginId(loginId);
            if (exist){
                resp.getWriter().print("exist");  //用户名已经存在
            }else{
                resp.getWriter().print("ok"); //用户名不存在
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
            try {
                resp.getWriter().print(throwables.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void message(HttpServletRequest req, HttpServletResponse resp) {
        try {


            MyPageInfo myPageInfo = Wrapper.wrapperMyPageInfo(req);
            long count = teacherDao.count(myPageInfo);//获得总记录数
            int totalPage =(int) (count/myPageInfo.getPageSize())
                    +(count% myPageInfo.getPageSize()==0?0:1);
            myPageInfo.setPageTotal(totalPage);//总页码数
            myPageInfo.setTotal(count);//总记录数

            List<Studentmessage> list = new ArrayList<>();
            if(count>0){
                list = teacherDao.listByPage(myPageInfo);
            }
            myPageInfo.setDataList(list);



            req.setAttribute("dataList",list);
            req.setAttribute("myPageInfo",myPageInfo);
//            req.getRequestDispatcher("/jsp/admin/main.jsp").forward(req,resp);

            req.getRequestDispatcher("/jsp/teacher/Customers.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void MyMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        try {
            List<Teachermessage> list = teacherDao.MyMessageList();
            req.setAttribute("list",list);
            req.getRequestDispatcher("/jsp/teacher/MyMessage.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
//    public void MyMessage(HttpServletRequest req, HttpServletResponse resp){
//        String userName = req.getParameter("userName");
//        //创建session对象
//        HttpSession session = req.getSession();
//        //把用户数据保存在session域对象中
//        session.setAttribute("userName", userName);
//        try {
//            Teachermessage teachermessage= teacherDao.myMessage((String) session.getAttribute("userName"));
//            req.setAttribute("data",teachermessage);
//            System.out.println(teachermessage);
////            resp.sendRedirect("jsp/teacher/MyMessage");
//            req.getRequestDispatcher("jsp/teacher/MyMessage").forward(req,resp);
//        } catch (Exception throwables) {
//            throwables.printStackTrace();
//        }
//
//    }

    public void saveMyMessage(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String userName = req.getParameter("userName");
        teacherDao.savemyMessage(userName);
        resp.getWriter().print("ok");
    }

    @Override
    public void StudentScore(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        String id = req.getParameter("id");
        StudentScore studentScore = teacherDao.selectScoreById(Integer.parseInt(id));
        req.setAttribute("data",studentScore);
        req.getRequestDispatcher("/jsp/teacher/StudentScoreEdit.jsp").forward(req,resp);
    }
    public void doEditStudentScore(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        StudentScore studentScore = StudentScoreWrapper.ScoreWrapper(req);
        teacherDao.doEditStudentScoreMessage(studentScore);
        resp.getWriter().print("ok");
    }
    public void StudentScoreMessage(HttpServletRequest req, HttpServletResponse resp) {
        try {
            List<StudentScore> list = teacherDao.studentScoreList();
            req.setAttribute("list",list);
            req.getRequestDispatcher("/jsp/teacher/StudentScore.jsp").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public void doAddStudentMessage(HttpServletRequest req, HttpServletResponse resp) throws IOException, SQLException {
        Studentmessage studentmessage = StudentMessageWrapper.wrapper(req);
        studentDao.savem(studentmessage);
        resp.getWriter().print("ok");
    }

    @Override
    public void deleteStudentMessage(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        String id = req.getParameter("id");
        teacherDao.deleteStudentMessage(Integer.valueOf(id));
        resp.sendRedirect("/teacher?method=studentMessage");
    }
}
